import { ADD_NUMBER, SUB_NUMBER, CHANGE_NAME, CHANGE_BANNER }  from "./constants"
import axios from "axios"

const changeNameAction = (name) => ({
  // type: "change_name",
  type: CHANGE_NAME,
  name
})

const changeBannersAction = (banner) => ({
  // type: "change_Banner",
  type: CHANGE_BANNER,
  banner
})

const addNumberAction = (num) => ({
  // type: "add_number",
  type: ADD_NUMBER,
  num
})

const subNumberAction = (num) => ({
  // type: "sub_number",
  type: SUB_NUMBER,
  num
})


// 调用 fetchNetWorkDataAction 不在返回一个action对象，而是一个action函数
const fetchNetWorkDataAction = () => {

  // foo 函数就是要被派发出去的函数，派发后他会自动执行
  function foo(dispatch, getState){
    axios.get("http://123.207.32.32:8000/home/multidata").then(res => {
      const banners = res.data.data.banner.list
      // 网络请求成功后，通过dispatch派发一个action对象，给store赋值
      dispatch(changeBannersAction(banners))
    })
  }

  return foo
}

export {changeNameAction, addNumberAction, subNumberAction, changeBannersAction, fetchNetWorkDataAction}